package com.shoppingmall.fragment;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.zyh.toolslib.base.BaseFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.shoppingmall.R;
import com.shoppingmall.activity.mine.MineCollectActivity;
import com.shoppingmall.bean.CollectGoodsBean;
import com.shoppingmall.bean.DealersBean;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * 2020/3/15
 * 描述：我的收藏商品
 */
public class MineCollectGoodsFragment extends BaseFragment implements MineCollectActivity.OnModifyListener {


    @BindView(R.id.rv_content_list)
    RecyclerView rvContentList;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;

    private boolean modifyState;//是否是编辑状态

    private List<CollectGoodsBean> mData = new ArrayList<>();
    private BaseQuickAdapter<CollectGoodsBean, BaseViewHolder> mAdapter;

    private List<CollectGoodsBean> selectData = new ArrayList<>();

    @Override
    protected void initView(View rootView) {

    }

    @Override
    protected void initData() {
        smartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

            }
        });

        for (int i = 0; i < 5; i++) {
            mData.add(new CollectGoodsBean());
        }
        mAdapter = new BaseQuickAdapter<CollectGoodsBean, BaseViewHolder>(R.layout.item_mine_collect_goods, mData) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, CollectGoodsBean item) {
                helper.setVisible(R.id.cb_collect, modifyState);
                helper.setGone(R.id.cb_collect, modifyState);
                helper.setOnCheckedChangeListener(R.id.cb_collect, new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        item.setChoosed(b);
                    }
                });
            }
        };
        rvContentList.setLayoutManager(new LinearLayoutManager(mActivity));
        rvContentList.setAdapter(mAdapter);
        ((MineCollectActivity) getActivity()).setOnModifyListener(this);


    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_recycleview;
    }

    @Override
    public void modify(boolean isModifyState) {
        modifyState = isModifyState;
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void cancel() {
        selectData.clear();
        for (int i = 0; i < mData.size(); i++) {
            CollectGoodsBean bean = mData.get(i);
            if (bean.isChoosed()) {

                selectData.add(bean);
            }
        }
        ToastUtils.showShort("要删除" + selectData.size() + "个");
    }

//    //  定义用来与外部activity交互，获取到宿主activity
//    private GoodsCollectFragmentListterner listterner;
//
//    // 1定义了所有activity必须实现的接口方法
//    public interface GoodsCollectFragmentListterner {
//        void process(List<CollectGoodsBean> collectGoodsBeanList);
//    }
//
//    // 当Fragment被加载到宿主activity的时候会被回调
//    @Override
//    public void onAttach(@NonNull Context context) {
//        super.onAttach(context);
//
//        if (context instanceof GoodsCollectFragmentListterner) {
//            listterner = (GoodsCollectFragmentListterner) context; // 2.2 获取到宿主activity并赋值
//        } else {
//            throw new IllegalArgumentException("activity must implements FragmentListterner ");
//        }
//    }
//
//    //把传递进来的activity对象释放掉
//    @Override
//    public void onDetach() {
//        super.onDetach();
//        listterner = null;
//    }

}
